import {
  editwifibatch,
  editcssendbatch,
  editmqttbatch,
  editcmdbatch,
  editchangetempbatch,
  editappupgradebatch,
  editvolumebatch,
  bindcsimage,
  bindcsvideo,
  ColorTerminalOnLineJob,
} from '@/api/mupro'
import { parseTime } from '@/utils'

const save = async (
  result: Array<any>,
  type: string,
  multipleSelection: Array<any>
) => {
  let fun: any
  const params: any = {}
  switch (type) {
    case 'push':
      fun = editcssendbatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      break
    case 'wifi':
      fun = editwifibatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      result.map((item: any) => {
        params[item.field] = item.model
      })
      break
    case 'server':
      fun = editmqttbatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      result.map((item: any) => {
        params[item.field] = item.model
      })
      break
    case 'cache':
      fun = editcmdbatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      result.map((item: any) => {
        params[item.field] = item.model
      })
      break
    case 'mode':
      fun = editchangetempbatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      result.map((item: any) => {
        params[item.field] = item.model
      })
      break
    case 'screen':
      fun = editappupgradebatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      result.map((item: any) => {
        params[item.field] = item.model
      })
      break
    case 'update':
      fun = editappupgradebatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      result.map((item: any) => {
        params[item.field] = item.model
      })
      break
    case 'volume':
      fun = editvolumebatch
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.ID
        })
        .join(',')
      result.map((item: any) => {
        params[item.field] = item.model
      })
      break
    case 'bindImg':
      fun = bindcsimage
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.UUID
        })
        .join(';')
      params.imagelist = result
        .map((item: any) => {
          return item.CSImageName
        })
        .join(',')
      break
    case 'bindVideo':
      fun = bindcsvideo
      params.cslist = multipleSelection
        .map((item: any) => {
          return item.UUID
        })
        .join(';')
      result.map((item, index) => {
        params[`videoList${index + 1}`] = item.videos
          .map((video: any) => {
            return video.CSVideoName
          })
          .join(';')
        params[`txt_search_startdate${index + 1}`] = parseTime(
          item.times[0],
          '{y}-{m}-{d} {h}:{i}:{s}'
        )
        params[`txt_search_enddate${index + 1}`] = parseTime(
          item.times[1],
          '{y}-{m}-{d} {h}:{i}:{s}'
        )
      })
      break
    case 'refresh':
      fun = ColorTerminalOnLineJob
      break
  }
  const { data, code, message } = await fun(params)
  return {
    data,
    code,
    message,
  }
}

export { save }
